<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 10] 10.2 System Properties</TITLE>
<META NAME="author" CONTENT="Mark Grand and Jonathan Knudsen">
<META NAME="date" CONTENT="Fri Aug  8 16:19:24 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java Fundamental Classes Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Fundamental Classes Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 10<br>Accessing the Environment</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch10_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JFC-CH-10-SECT-2">10.2 System Properties</A></h2>

<P CLASS=para>
<A NAME="CH10.SYS"></A><A NAME="CH10.SYS2"></A>
System properties provide a mechanism for getting information
about the environment. You can get the value of a system property by
passing its name to the <tt CLASS=literal>System.getProperty(String)</tt>
method. This method returns the value of the named property as a
<tt CLASS=literal>String</tt>, or it returns <tt CLASS=literal>null</tt> if
the property is not defined. Since it is common to assume a default
value if a property is not specified, there is also a
<tt CLASS=literal>System.getProperty(String,</tt>
<tt CLASS=literal>String)</tt> method that takes the name of a property
and a default <tt CLASS=literal>String</tt> value to return if the
property is not defined.

<P CLASS=para>
<A HREF="ch10_02.htm#JFC-CH-10-TAB-1">Table 10.1</A> lists the standard system properties for 
a Java environment. Many of these properties are guaranteed to be defined 
in any Java environment. Note, however, that untrusted applets aren't 
allowed to access many of these properties. 

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="JFC-CH-10-TAB-1">Table 10.1: Standard System Properties</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="LEFT">

<P CLASS=para>
Property Name</TH>
<TH ALIGN="LEFT">

<P CLASS=para>
Description</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>file.encoding</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The character encoding for the default locale ( Java 1.1 only)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>file.encoding.pkg</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The package that contains the converters that handle converting between 
local encodings and Unicode ( Java 1.1 only)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>file.separator</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The platform-dependent file separator 
(e.g., "<tt CLASS=literal>/</tt>" on UNIX, "<tt CLASS=literal>\</tt>" 
for Windows)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>java.class.path</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The value of the <tt CLASS=literal>CLASSPATH</tt> 
environment variable</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>java.class.version</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The version of the Java API</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>java.compiler</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The just-in-time compiler to use, if any. The <I CLASS=emphasis>java</I> 
interpreter provided with the JDK initializes this property from the environment 
variable <tt CLASS=literal>JAVA_COMPILER</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>java.home</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The directory in which Java is installed</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>java.version</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The version of the Java interpreter</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>java.vendor</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
A vendor-specific string</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>java.vendor.url</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
A vendor URL</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>line.separator</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The platform-dependent line separator (e.g.,
"<tt CLASS=literal>\n</tt>" on UNIX,
"<tt CLASS=literal>\r\n</tt>" for Windows)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>os.name</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The name of the operating system</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>os.arch</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The system architecture</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>os.version</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The operating system version</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>path.separator</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The platform-dependent path separator 
(e.g., "<tt CLASS=literal>:</tt>" on UNIX, "<tt CLASS=literal>,</tt>" 
for Windows)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>user.dir</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The current working directory 
when the properties were initialized</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>user.home</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The home directory of the current user</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>user.language</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The two-letter language code of the default locale ( Java 1.1 only)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>user.name</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The username of the current user</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>user.region</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The two-letter country code of the default locale ( Java 1.1 only)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="LEFT">

<P CLASS=para>
<tt CLASS=literal>user.timezone</tt></TD>
<TD ALIGN="LEFT">

<P CLASS=para>
The default time zone ( Java 1.1 only)</TD>
</TR>
</TABLE>
<P>
</DIV>
<P CLASS=para>
The Java API also provides some convenience methods for getting the value 
of a system property that should be interpreted as a data type other than 
<tt CLASS=literal>String</tt>: 

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem><tt CLASS=literal></tt><tt CLASS=literal>Boolean.getBoolean()</tt> 
returns the value of a named system property as a <tt CLASS=literal>boolean</tt>. 

<P>
<li CLASS=listitem><tt CLASS=literal>Color.getColor()</tt> returns 
a <tt CLASS=literal>Color</tt> object that represents 
the color specified by the value of a named system property interpreted 
as an RGB value. For example, the value <tt CLASS=literal>0xFFFFFF</tt> 
is white, <tt CLASS=literal>0xFF000</tt> is red, <tt CLASS=literal>0x00FF00</tt> 
is green, and <tt CLASS=literal>0x0000FF</tt> is blue. 

<P>
<li CLASS=listitem>
<tt CLASS=literal>Font.getFont()</tt> returns a <tt CLASS=literal>Font</tt>
object that is mapped to a font in the native windowing system. If the
string passed to <tt CLASS=literal>getFont()</tt> is
<tt CLASS=literal>"poster"</tt>, the method uses the value of the system
property <tt CLASS=literal>awt.font.poster</tt> as the name of the native
font.

<P CLASS=para>
By default, the font style is plain and the font size is 12 points. If
the font name is prefixed with <tt CLASS=literal>bold-</tt>,
<tt CLASS=literal>italic-</tt> or <tt CLASS=literal>bolditalic-</tt>, that
style is used instead. If the font name is prefixed with a size and a
<tt CLASS=literal>-</tt>, that size is used instead. If both style and
size are specified, style must come first. For example, passing
<tt CLASS=literal>"italic-14-timesRoman"</tt> to
<tt CLASS=literal>getFont()</tt> causes it to return a
<tt CLASS=literal>Font</tt> object that uses the native font identified by
the system property <tt CLASS=literal>awt.font.timesRoman</tt>. That font
should be an italic, 14-point, TimesRoman font.
<P>
<li CLASS=listitem><tt CLASS=literal>Integer.getInteger()</tt> returns 
the value of a named system property as an <tt CLASS=literal>int</tt>. 

<P>
<li CLASS=listitem><tt CLASS=literal>Long.getLong()</tt> returns 
the value of a named system property as a <tt CLASS=literal>long</tt>. 

<P CLASS=para>
There are two built-in mechanisms for setting system properties. Note that 
you can use these mechanisms to set the standard system properties or to 
define specific system properties for your own application. 

<P>
<li CLASS=listitem>You can define properties from the command line of the Java virtual 
machine using the <tt CLASS=literal>-D</tt> command 
line option. For example, to define a property named <tt CLASS=literal>time.server</tt> 
with the value <tt CLASS=literal>tm02</tt>, you can 
invoke the interpreter like this: 

<DIV CLASS=screen>
<P>
<PRE>
C:\&gt; java -Dtime.server=tm02
</PRE>
</DIV>

<P>
<li CLASS=listitem>You can define any number of system properties using <tt CLASS=literal>-D</tt>, 
as long as each property is specified with its own <tt CLASS=literal>-D</tt> 
option. 

<P>
</UL>
<P CLASS=para>
You can programmatically define properties by calling the
<tt CLASS=literal>System.setProperties()</tt> method. The
<tt CLASS=literal>Properties</tt> object that you pass to
<tt CLASS=literal>System.setProperties()</tt> becomes the new source for
all system property values.

<P CLASS=para>
If a program is running in a browser or other environment that has a
<tt CLASS=literal>SecurityManager</tt> installed, it may be denied any
access to system properties.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch10_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>I/O</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Environment Variables</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
